package com.son.hotel.controller;


import cn.hutool.core.bean.BeanUtil;
import com.son.hotel.common.ErrorCode;
import com.son.hotel.common.Result;
import com.son.hotel.domain.Customers;
import com.son.hotel.domain.Dishes;
import com.son.hotel.domain.Orders;
import com.son.hotel.execption.BusinessException;
import com.son.hotel.response.OrderAccountResp;
import com.son.hotel.response.OrderAndDishResp;
import com.son.hotel.service.CustomersService;
import com.son.hotel.service.DishesService;
import com.son.hotel.service.OrdersService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/menu")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private CustomersService customersService;

    @Autowired
    private DishesService dishesService;

    @Operation(summary = "预定菜品-顾客")
    @PostMapping("/order")
    public Result<Boolean> addOrder(@RequestBody Orders orders){
        log.info("orders:{}",orders);
        if (BeanUtil.isEmpty(orders)){
            throw new BusinessException(ErrorCode.NULL_ERROR,"参数为空");
        }
        int cost = (orders.getCost()) * (orders.getQuantity());
        LocalDateTime time = LocalDateTime.now();
        log.info("currentTime:{}",time);
        //设置点餐时间
        orders.setTime(time);
        //设置点餐花费
        orders.setCost(cost);
        //插入点餐表
        boolean save = ordersService.save(orders);
        if (save){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"插入失败");
        }
        return Result.success("200",true);
    }

    /**
     * 获取订单信息
     * @param customerId
     * @return
     */
    @Operation(summary = "查询消费记录-顾客")
    @GetMapping("/order/info/{customerId}")
    public Result<List<OrderAndDishResp>> getOrderInfo(@PathVariable int customerId){
        if (customerId <= 0){
            throw new BusinessException(ErrorCode.PARAMETER_ERROR);
        }
        List<OrderAndDishResp> orderAndDishRespList = ordersService.getOrder(customerId);
        log.info("orderAndDishResp:{}",orderAndDishRespList);
        return Result.success("200",orderAndDishRespList);
    }

    /**
     * 获取订单信息(admin)
     * @return
     */
    @Operation(summary = "消费记录-查询-admin")
    @GetMapping("/order/info/list")
    public Result<List<OrderAndDishResp>> getOrderInfo(){
        List<Orders> ordersList = ordersService.list();
        List<OrderAndDishResp> list = new ArrayList<>();
        for (Orders orders : ordersList) {
            OrderAndDishResp orderAndDishResp = new OrderAndDishResp();
            //根据顾客id获取顾客信息
            Integer customerId = orders.getCustomerId();
            Customers customers = customersService.getById(customerId);
            //获取顾客姓名
            String name = customers.getName();
            //根据菜品id获取菜品信息
            Integer dishId = orders.getDishId();
            Dishes dishes = dishesService.getById(dishId);
            String dishName = dishes.getName();

            orderAndDishResp.setDishName(dishName);
            orderAndDishResp.setName(name);
            orderAndDishResp.setOrders(orders);
            log.info("orderAndDishResp:{}",orderAndDishResp);

            list.add(orderAndDishResp);
        }
        return Result.success("200",list);
    }

    /**
     * 列出销量排名前五的菜品
     * @return
     */
    @Operation(summary = "统计排名销售量前五的菜品-admin")
    @GetMapping("/order/account")
    public Result<List<OrderAccountResp>> listOrderAccountTop5(){
        List<OrderAccountResp> orderAccountResp = ordersService.listOrderAccountTop5();
        return Result.success("200",orderAccountResp);
    }
}
